package com.qy139.auth.handler;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.qy139.auth.util.MapperUtils;
import com.qy139.auth.util.OkHttpClientUtil;
import com.qy139.util.Result;
import lombok.SneakyThrows;
import okhttp3.Response;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Component
public class MySuccessHandler implements AuthenticationSuccessHandler {


    @Resource
    private UserDetailsService userDetailsService;
    @SneakyThrows
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
// 成功
        String username = httpServletRequest.getParameter("username");// 获取username
       // System.out.println("对应的用户名的信息是:"+username);
        String password = httpServletRequest.getParameter("password");// 获取username

        // 资源的值   所有的资源

        User userDetails = (User) userDetailsService.loadUserByUsername(username);
        //资源
        Collection<GrantedAuthority> authorities = userDetails.getAuthorities();
        System.out.println("该用户的资源的信息是:"+authorities);
        // 生成一个token
        ObjectMapper objectMapper = new ObjectMapper();
        Result result = new Result();
        result.setCode(400);
        result.setMsg("登录成功");

        Map map = new HashMap();
        map.put("username",username);
        map.put("password",password);
        map.put("client_id", "client_id");
        map.put("client_secret","secret");
        map.put("grant_type","password");

        // token 获取出来
        //localhost:8088/oauth/token?username=张三&password=123456&client_id=client_id&client_secret=secret&grant_type=password
        Response response = OkHttpClientUtil.getInstance().postData("http://localhost:8088/oauth/token", map);

        String s1 = Objects.requireNonNull(response.body()).string();
        System.out.println("response的信息==="+s1);
        Map<String, Object> jsonMap = MapperUtils.json2map(s1);
        Object token = jsonMap.get("access_token");// 对应的token的值
        System.out.println("对应的token的值=====:"+token);

        Map maps = new HashMap();
        maps.put("token",token);
        maps.put("qx",authorities);
        // result
        result.setData(maps);
        String s = objectMapper.writeValueAsString(result);
        //json数据    uft-8
        httpServletResponse.setContentType("application/json;charset=utf8");
        // 响应
        PrintWriter out = httpServletResponse.getWriter();
        out.print(s);
        out.flush();
        out.close();
    }
}
